package com.jsrj.controller;

import com.jsrj.common.annotation.Log;
import com.jsrj.common.config.RuoYiConfig;
import com.jsrj.common.core.controller.BaseController;
import com.jsrj.common.core.domain.AjaxResult;
import com.jsrj.common.core.page.TableDataInfo;
import com.jsrj.common.enums.BusinessType;
import com.jsrj.common.utils.file.FileUploadUtils;
import com.jsrj.common.utils.poi.ExcelUtil;
import com.jsrj.domain.Image;
import com.jsrj.domain.ImgAdv;
import com.jsrj.service.IImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2022-09-03
 */
@RestController
@RequestMapping("/system/image")
public class ImageController extends BaseController
{
    @Autowired
    private IImageService imageService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:image:list')")
    @GetMapping("/list")
    public TableDataInfo list(Image image)
    {
        startPage();
        List<Image> list = imageService.selectImageList(image);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:image:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Image image)
    {
        List<Image> list = imageService.selectImageList(image);
        ExcelUtil<Image> util = new ExcelUtil<Image>(Image.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:image:query')")
    @GetMapping(value = "/{imageId}")
    public AjaxResult getInfo(@PathVariable("imageId") Long imageId)
    {
        return AjaxResult.success(imageService.selectImageByImageId(imageId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:image:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestParam("file") MultipartFile file)
    {
        Image image = new Image();
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileNewName = null;
        try {
            fileNewName = FileUploadUtils.upload(filePath, file);
            image.setImageUrl(filePath+fileNewName.substring(15));
        } catch (IOException e) {
            e.printStackTrace();
        }
        imageService.insertImage(image);
        return AjaxResult.success(image.getImageId());
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:image:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Image image)
    {
        return toAjax(imageService.updateImage(image));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:image:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{imageIds}")
    public AjaxResult remove(@PathVariable Long[] imageIds)
    {
        return toAjax(imageService.deleteImageByImageIds(imageIds));
    }





    /**
     * 查询图片集合，同时查询每个图片对应的软文
     *
     * @return
     */
    /**
     * showdoc
     * @catalog 道路救援小程序接口/图片image
     * @title 查询图片集合，同时查询每个图片对应的软文
     * @description 查询图片集合，同时查询每个图片对l应的软文
     * @method get
     * @url http://localhost:8080/system/image/selectImgAndAdv
     * @header token 可选 string 设备token
     * @return {"error_code":0,"data":{}
     * @remark 测试
     * @number 1
     */
    @GetMapping("/selectImgAndAdv")
    public AjaxResult selectImageAndAver(){
        List<ImgAdv> imageAdv = imageService.selectImageAndAdvertorial();
        return AjaxResult.success(imageAdv);
    }









}
